package com.faner.fast.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.faner.fast.annotation.AnonymousAccess;
import com.faner.fast.annotation.SysLog;
import com.faner.fast.base.PageDTO;
import com.faner.fast.upms.domain.vo.RoleVO;
import com.faner.fast.upms.model.SysRoleDO;
import com.faner.fast.upms.service.SysRoleMenuService;
import com.faner.fast.upms.service.SysRoleService;
import com.faner.fast.util.PageUtil;
import com.faner.fast.util.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author faner
 * @since 2021-01-22
 */
@RestController
@RequiredArgsConstructor
@Api(value = "role", tags = "系统-角色管理模块")
@ApiSupport(order = 20)
public class RoleController {

    private final SysRoleService sysRoleService;
    private final SysRoleMenuService sysRoleMenuService;

    /**
     * 获取角色列表
     * @return 角色列表
     */

    @ApiOperation(value = "获取角色列表")
    @ApiOperationSupport(order = 10,author = "faner")
    @GetMapping("/role/list")
    public R listRoles() {
        return R.ok(sysRoleService.list(Wrappers.emptyWrapper()));
    }

    /**
     * 分页查询角色信息
     * @param page 分页对象
     * @return 分页对象
     */
    @AnonymousAccess
    @ApiOperation(value = "分页查询角色信息")
    @ApiOperationSupport(order = 20,author = "faner")
    @GetMapping(value = {"/role/page","/modeler/role/page"})
    public R getRolePage(@Validated PageDTO page) {
        Page<SysRoleDO> sysRolePage = sysRoleService.getSysRolePage(page);
        return R.ok(PageUtil.resetPage(sysRolePage));
    }
    /**
     * 通过ID查询角色信息
     *
     * @param id ID
     * @return 角色信息
     */
    @ApiOperation(value = "详情")
    @ApiOperationSupport(order = 30,author = "faner")
    @GetMapping("/role/detail")
    public R getById(@RequestParam Long id) {
        return R.ok(sysRoleService.getById(id));
    }

    /**
     * 添加角色
     * @param sysRole 角色信息
     * @return success、false
     */
    @PreAuthorize("@pms.hasPermission('sys_role_add')")
    @SysLog("添加角色")
    @ApiOperation(value = "添加角色")
    @ApiOperationSupport(order = 40,author = "faner")
    @PostMapping("/role/save")
    public R save(@Validated @RequestBody SysRoleDO sysRole) {
        return R.ok(sysRoleService.save(sysRole));
    }

    /**
     * 修改角色
     * @param sysRole 角色信息
     * @return success/false
     */
    @PreAuthorize("@pms.hasPermission('sys_role_edit')")
    @SysLog("修改角色")
    @ApiOperation(value = "修改角色")
    @ApiOperationSupport(order = 50,author = "faner")
    @PostMapping("/role/update")
    public R update(@Validated @RequestBody SysRoleDO sysRole) {
        return R.ok(sysRoleService.updateById(sysRole));
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PreAuthorize("@pms.hasPermission('sys_role_del')")
    @SysLog("删除角色")
    @ApiOperation(value = "删除角色")
    @ApiOperationSupport(order = 60,author = "faner")
    @PostMapping("/role/delete")
    public R removeById(@PathVariable Long id) {
        return R.ok(sysRoleService.removeRoleById(id));
    }



    /**
     * 更新角色菜单
     * @param roleVo 角色对象
     * @return success、false
     */
    @PreAuthorize("@pms.hasPermission('sys_role_perm')")
    @SysLog("更新角色菜单")
    @ApiOperation(value = "更新角色菜单")
    @ApiOperationSupport(order = 70,author = "faner")
    @PutMapping("/role/menu")
    public R saveRoleMenus(@RequestBody RoleVO roleVo) {
        SysRoleDO sysRole = sysRoleService.getById(roleVo.getRoleId());
        return R.ok(sysRoleMenuService.saveRoleMenus(roleVo.getRoleId(), roleVo.getMenuIds()));
    }
}
